package com.wxsh.birthday;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.nlf.calendar.Lunar;
import com.nlf.calendar.LunarMonth;
import com.nlf.calendar.LunarYear;
import com.nlf.calendar.Solar;

public class HappyDay {

	public static void main(String[] args) throws Exception {
		int y = 1987;
		int m = 10;
		int d = 1;

		Lunar lu = Lunar.fromYmd(y, m, d);
		Lunar today = Lunar.fromDate(new Date());
		Solar so = lu.getSolar();

		String ss = so.toString();
		String xz = so.getXingZuo();
		System.out.println(xz);
		System.out.println(ss);
		System.out.println(lu.getWeekInChinese());
		System.out.println(lu.getYearShengXiao());
		System.out.println(today.getWeekInChinese());
		System.out.println(today.getYearShengXiao());
		System.out.println(today.toFullString());

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date birth = sdf.parse(ss + " 00:00:00");
		int days = daysBetween(birth, new Date());
		int years = days / 365;
		int dd = days % 365;
		System.out.println("健康平安" + days + "天");
		System.out.println(years);
		System.out.println(dd);

		int sum = getLunarDaysOfYear(2020, 10, 10);
		int sss = getLunarDaysOfYear(today.getYear(), today.getMonth(), today.getDay());
		System.out.println(sum);
		System.out.println(sss);
		System.out.println(sss-sum);
		
		if(y>today.getYear()){
			System.out.println("错误时间");
		}
		
		int ty = today.getYear();
		int tm = today.getMonth();
		int td = today.getDay();
		
		if((m*100+d)>(tm*100+td)){
			//生日时间还未到
			//上一年过了多少天, 从生日那天算起
			int yg1 = getLunarDaysOfYear(ty-1)-getLunarDaysOfYear(ty-1, m, d);
			//今年过了多少天
			int yg2 = getLunarDaysOfYear(ty, tm, td);
			int yg = yg1+yg2;
			System.out.println(yg);
		}else if((m*100+d)==(tm*100+td)){
			//今天生日
			int old = (ty-y)+1;
			System.out.println("今天"+old+"岁生日, 开启新的旅程!");
		}else{
			//生日时间已过
			int yg1 = getLunarDaysOfYear(y, m, d)-1;
			int yg2 = getLunarDaysOfYear(ty, tm, td);
			int yg = (yg2-yg1)+1;
			System.out.println("已过"+yg);
		}
		
		
	}

	/**
	 * 到今天总共多少天--阴历
	 * @param year
	 * @param month--(不包含)
	 * @param day--(包含)
	 * @return
	 */
	public static int getLunarDaysOfYear(int year, int month, int day) {
		LunarYear ly = LunarYear.fromYear(year);
		List<LunarMonth> months = ly.getMonths();
		int days = 0;
		int idx = 1;
		for (LunarMonth lunarMonth : months) {
			int y = lunarMonth.getYear();
			if(y==year && idx<month){
				int d = lunarMonth.getDayCount();
				System.out.println(idx);
				days+=d;
				idx++;
			}
		}
		days+=day;
		return days;
	}
	
	/**
	 * 到这个月底总共多少天--阴历
	 * @param year
	 * @param month--(包含)
	 * @return
	 */
	public static int getLunarDaysOfYear(int year, int month) {
		LunarYear ly = LunarYear.fromYear(year);
		List<LunarMonth> months = ly.getMonths();
		int days = 0;
		int idx = 1;
		for (LunarMonth lunarMonth : months) {
			int y = lunarMonth.getYear();
			if(y==year && idx<=month){
				int d = lunarMonth.getDayCount();
				days+=d;
				idx++;
			}
		}
		return days;
	}
	
	/**
	 * 今年总共多少天--阴历
	 * @param year
	 * @return
	 */
	public static int getLunarDaysOfYear(int year) {
		LunarYear ly = LunarYear.fromYear(year);
		List<LunarMonth> months = ly.getMonths();
		int days = 0;
		for (LunarMonth lunarMonth : months) {
			int y = lunarMonth.getYear();
			if(y==year){
				int d = lunarMonth.getDayCount();
				days+=d;
			}
		}
		return days;
	}

	public static int daysBetween(Date birth, Date now) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(birth);
		long time1 = cal.getTimeInMillis();
		cal.setTime(now);
		long time2 = cal.getTimeInMillis();
		long days = (time2 - time1) / (24 * 60 * 60 * 1000);
		return Integer.parseInt(String.valueOf(days + 1));
	}
}
